package com.dq.service.messsge.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;

import com.dq.domain.message.oneself.ApprovalMessage;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.message.oneself.NoticeMessage;
import com.dq.domain.message.oneself.centre.ApprovalUserCentre;
import com.dq.domain.orders.OrgOrder;
import com.dq.domain.org.Org;
import com.dq.domain.sys.role.SysUserRole;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.messsge.ApprovalMessageService;
import com.dq.service.sys.user.BossUserService;
import com.dq.service.sys.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 *
 */
@Service
public class ApprovalMessageServiceImpl extends BaseRepositories implements ApprovalMessageService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private BossUserService bossUserService;

    /**
     * 添加消息实体
     * @param orgOrderId 采购单ID
     * @return
     * @throws AdminException
     */
    @Override
    public ApprovalMessage save(Long orgOrderId)  throws AdminException {
        ApprovalMessage approvalMessage = new ApprovalMessage();
        approvalMessage.setMessageStatus(NoticeMessage.MessageStatus.noSee);
        approvalMessage.setMessageType(NoticeMessage.MessageType.approval);

        approvalMessage.setVersion(Constant.VERSION_VALUE);
        approvalMessage.setDeleted(false);
        approvalMessage.setCreateTime(System.currentTimeMillis());
        approvalMessage.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        approvalMessage.setCreateUser(user);
        approvalMessage.setUpdateUser(user);

        OrgOrder orgOrder = dao.get(OrgOrder.class, orgOrderId);
        if(orgOrder == null || orgOrder.getId() == null) {
            throw new AdminException("无对应采购单");
        }
        approvalMessage.setOrgOrder(orgOrder);
        if(orgOrder.getOrg() == null) {
            throw new AdminException("门店信息为空");
        }
        approvalMessage.setTheme(orgOrder.getOrg().getSimpleName());
        approvalMessage.setOrg(orgOrder.getOrg());

        dao.saveOrModify(approvalMessage);
        return approvalMessage;
    }

    /**
     * 添加 采购用户关联 基本属性配置
     */
    @Override
    public ApprovalUserCentre objectCentreSaveBase(ApprovalUserCentre approvalUserCentre) throws AdminException {
        if(approvalUserCentre.getMessageStatus() == null) {
            approvalUserCentre.setMessageStatus(Message.MessageStatus.noSee);
        }

        approvalUserCentre.setVersion(Constant.VERSION_VALUE);
        approvalUserCentre.setDeleted(false);
        approvalUserCentre.setCreateTime(System.currentTimeMillis());
        approvalUserCentre.setLastModifyTime(System.currentTimeMillis());

        return approvalUserCentre;
    }

    /**
     * 修改采购消息, 不修改 采购审批消息用户关联
     */
    @Override
    public ApprovalMessage update(ApprovalMessage approvalMessage) throws AdminException {
        approvalMessage.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            approvalMessage.setUpdateUser(user);
        }

        dao.saveOrModify(approvalMessage);

        return approvalMessage;
    }

    /**
     * 给 所有负责本门店的 boss管理员发送 采购消息
     */
    @Override
    public ApprovalMessage messageToBoss(Long orgOrderId)  throws AdminException {
        // 保存 采购单消息实体
        ApprovalMessage approvalMessage = save(orgOrderId);
        // 获取 负责本门店 的所有boss
        List<BossUser> bossByOrgList = bossUserService.getBossByOrg((Org)approvalMessage.getOrg());
        // 采购单消息实体 关联 消息接收者
        List<ApprovalUserCentre> approvalUserCentreList = new ArrayList<>();
        for (BossUser bossUser : bossByOrgList) {
            ApprovalUserCentre approvalUserCentre = new ApprovalUserCentre();
            approvalUserCentre = objectCentreSaveBase(approvalUserCentre);
            approvalUserCentre.setApprovalMessage(approvalMessage);
            approvalUserCentre.setCaseUser(bossUser);

            approvalUserCentreList.add(approvalUserCentre);
        }

        dao.saveOrModifyList(approvalUserCentreList);

        return approvalMessage;
    }
    /**
     *  给 所有 [仓库/财务] 发采购消息
     */
    @Override
    public void messageToRole(Long orgOrderId, int i) throws AdminException {
        // 保存 采购单消息实体
        ApprovalMessage approvalMessage = save(orgOrderId);
        // 获取 所有 [仓库/财务]
        Map<String, Object> query = new HashMap<>(16);
        query.put("sysRole_code", Constant.ROLE_CODE_LIST.get(i));
        List<SysUserRole> sysUserList = dao.find(dao.query2Hql(SysUserRole.class, query), query);
        // 采购单消息实体 关联 消息接收者
        List<ApprovalUserCentre> approvalUserCentreList = new ArrayList<>();
        for (SysUserRole userRole : sysUserList) {
            ApprovalUserCentre approvalUserCentre = new ApprovalUserCentre();
            approvalUserCentre = objectCentreSaveBase(approvalUserCentre);
            approvalUserCentre.setApprovalMessage(approvalMessage);
            approvalUserCentre.setCaseUser(userRole.getSysUser());

            approvalUserCentreList.add(approvalUserCentre);
        }
        dao.saveOrModifyList(approvalUserCentreList);
    }
    /**
     *  给 采购单下单人 发采购消息
     */
    @Override
    public void messageToBuyer(Long orgOrderId) throws AdminException {
        // 保存 采购单消息实体
        ApprovalMessage approvalMessage = save(orgOrderId);
        // 采购单消息实体 关联 消息接收者
        ApprovalUserCentre approvalUserCentre = new ApprovalUserCentre();
        approvalUserCentre = objectCentreSaveBase(approvalUserCentre);
        approvalUserCentre.setApprovalMessage(approvalMessage);
        approvalUserCentre.setCaseUser(approvalMessage.getOrgOrder().getStaffUser());

        dao.saveOrModify(approvalUserCentre);
    }
}

